package com.example.myandroidtest.ui.component.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myandroidtest.databinding.ActivityMusicBinding;

public class MusicActivity extends AppCompatActivity {

    ActivityMusicBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMusicBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initView();
    }

    private void initView() {
        binding.toolbar.ivBack.setOnClickListener(v -> finish());
        binding.toolbar.tvTitle.setText("音乐播放");

        binding.btnStart.setOnClickListener(v -> {
            Intent intent = new Intent(this, MusicService.class);
            startService(intent);
//            startForegroundService(intent);
        });

        binding.btnPlay.setOnClickListener(v -> {
            Intent intent = new Intent(this, MusicService.class);
            intent.setAction(MusicService.ACTION_PLAY);
            startService(intent);
        });

        binding.btnPause.setOnClickListener(v -> {
            Intent intent = new Intent(this, MusicService.class);
            intent.setAction(MusicService.ACTION_PAUSE);
            startService(intent);
        });

        binding.btnStop.setOnClickListener(v -> {
            Intent intent = new Intent(this, MusicService.class);
            stopService(intent);
        });

        binding.btnBind.setOnClickListener(v -> {
            //绑定式  跟activity生命周期一致
            Intent intent = new Intent(this, MusicService.class);
            bindService(intent, new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    MusicService.MusicBinder musicBinder = (MusicService.MusicBinder) service;
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {

                }
            }, BIND_AUTO_CREATE);
        });
    }
}